
package com.phevos.knowledge.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.phevos.common.dbpool.exception.DBPoolException;
import com.phevos.common.util.SerialException;
import com.phevos.common.util.SerialManager;
import com.phevos.usercenter.client.service.Orgnization;
import com.phevos.usercenter.client.service.WebUtil;
import com.phevos.usercenter.client.vo.UserValue;
import com.phevos.web.db.PageQueryUtil;
import com.phevos.web.util.DAOUtil;
import com.phevos.web.util.DateUtil;
import com.phevos.web.util.DecodeUtil;
import com.phevos.web.util.RequestUtil;

public class QuestionAction extends DispatchAction
{
    //private static Logger logger = LoggerFactory.getLogger(QuestionAction.class.getName());

    /**
     * ��ѯ����
     * 
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     * @throws IOException
     * @throws ServletException
     * @throws SQLException 
     */
    public ActionForward queryQuestion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException, SQLException
    {
        String cat_id = RequestUtil.getParameterWhitDefault(request, "cat_id", "0");

        String sign = (String)request.getAttribute("sign");
        String summary = "";
        if (sign == null)
        {
            summary = RequestUtil.getParameterWhitDefault(request, "summary", "");
        }
        String pageNo = RequestUtil.getParameterWhitDefault(request, "pageNo", "1");
        String sqlWhere = "from tbfzyw_kb_questions where ";
        if (!cat_id.equals("0"))
        {
            sqlWhere += "cat_id=" + cat_id + " and ";
        }
        if (summary.length() != 0)
        {
            sqlWhere += "summary like '%" + summary + "%' and ";
        }
        sqlWhere += "1=1";
        String sql = "select count(*) " + sqlWhere;
        String total = (((Object[])DAOUtil.queryToArrList(sql).get(0))[0]).toString();
        sql = "select * " + sqlWhere;
        List result = PageQueryUtil.queryToMapList(sql, 15, Integer.parseInt(pageNo));
        Map typeNames = DAOUtil.queryToMap("select cat_id,cat_name from tbfzyw_categories");
        DecodeUtil.decodeString(result, "cat_id", "typeName", typeNames, "δ֪����");
        // Map userMap = UserUtil.queryIdAndNameToMap();
        List userList = Orgnization.getALlUser();
        HashMap userMap = new HashMap();
        for (int i = 0; userList != null && i < userList.size(); i++)
        {
            UserValue userValue = (UserValue)userList.get(i);
            userMap.put(userValue.getId().intValue() + "", userValue.getName());
        }
        DecodeUtil.decodeString(result, "user_id", "user_name", userMap, "δ֪�û�");
        DecodeUtil.decodeDate(result, "creation", "creation", null, "");
        DecodeUtil.decodeString(result, "published", "status", "0:δ����|1:����|2:ɾ��|3:�ѽ��", "δ����");
        sql = "select cat_id,cat_name from tbfzyw_categories";
        List types = DAOUtil.queryToMapList(sql);
        request.setAttribute("cat_id", cat_id);
        request.setAttribute("summary", summary);
        request.setAttribute("pageNo", pageNo);
        request.setAttribute("perPage", "15");
        request.setAttribute("total", total);
        request.setAttribute("totalPage", "" + (int)Math.ceil(Integer.parseInt(total) / 15.0d));
        request.setAttribute("result", result);
        request.setAttribute("types", types);
        request.setAttribute("currentUserId", WebUtil.getCurrentAccount(request));
        return mapping.findForward("mainQuestion");
    }

    /**
     * ɾ������
     * 
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     * @throws IOException
     * @throws ServletException
     * @throws SQLException 
     * @throws Exception
     */
    public ActionForward delQuestion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException, SQLException
    {
        String question_id = RequestUtil.getParameterWhitDefault(request, "question_id", "0");
        String sql = "delete from tbfzyw_kb_questions where question_id=" + question_id;
        DAOUtil.exeSqls(new String[]{sql});
        return queryQuestion(mapping, form, request, response);
    }

    /**
     * �ظ�����
     * 
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     * @throws IOException
     * @throws ServletException
     * @throws SQLException 
     * @throws Exception
     */
    public ActionForward answerQuestion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException, SQLException
    {
        String question_id = RequestUtil.getParameterWhitDefault(request, "question_id", "0");
        String sql = "select * from tbfzyw_kb_questions where question_id=" + question_id;
        List question = DAOUtil.queryToMapList(sql);
        sql = "select cat_id,cat_name from tbfzyw_categories";
        List types = DAOUtil.queryToMapList(sql);
        request.setAttribute("question", question);
        request.setAttribute("types", types);
        return mapping.findForward("answerQuestion");
    }

    /**
     * �༭�µ�����
     * 
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     * @throws IOException
     * @throws ServletException
     * @throws SQLException 
     * @throws Exception
     */
    public ActionForward editQuestion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException, SQLException
    {
        String sql  = "select cat_id,cat_name from tbfzyw_categories";
        List types = DAOUtil.queryToMapList(sql);
        request.setAttribute("types", types);
        return mapping.findForward("editQuestion");
    }

    /**
     * ��������
     * 
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     * @throws IOException
     * @throws ServletException
     * @throws DBPoolException 
     * @throws SerialException 
     * @throws SQLException 
     * @throws Exception
     */
    public ActionForward saveQuestion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException, SQLException, SerialException, DBPoolException
    {
        String typeid = RequestUtil.getParameterWhitDefault(request, "typeid", "0");
        String summary = RequestUtil.getParameterWhitDefault(request, "summary", "0");
        String details = RequestUtil.getParameterWhitDefault(request, "details", "0");
        String id = "" + SerialManager.getInstance().getNextId("kb_ques_id");
        // String user_id = UserUtil.getCurrentUserId(request);
        String user_id = WebUtil.getCurrentUserId(request);
        StringBuffer sql = new StringBuffer();
        sql.append("insert into tbfzyw_kb_questions(question_id,user_id,summary,details,cat_id,creation,published) values(");
        sql.append(id).append(",'").append(user_id).append("','");
        sql.append(summary).append("','");
        sql.append(details).append("',").append(typeid).append(",");
        sql.append(DateUtil.toDateSql(new java.util.Date()));
        sql.append(",1)");
        DAOUtil.exeSqls(new String[]{sql.toString()});
        request.setAttribute("sign", "parameter");
        return queryQuestion(mapping, form, request, response);
    }

}
